import {
	mapState
} from 'vuex'
import store from "@/store"
import {
	printData,
	printChargeData
} from '@/uni_modules/oneoil-newland'
// 尝试将用户在根目录中的store/index.js的vuex的state变量，全部加载到全局变量中
let $uStoreKey = [];
try {
	$uStoreKey = store.state ? Object.keys(store.state) : [];
} catch (e) {

}

module.exports = {
	beforeCreate() {
		// 将vuex方法挂在到$u中
		// 使用方法为：如果要修改vuex的state中的user.name变量为"史诗" => this.$u.vuex('user.name', '史诗')
		// 如果要修改vuex的state的version变量为1.0.1 => this.$u.vuex('version', '1.0.1')
		uni.$u.vuex = (name, value) => {
			this.$store.commit('$uStore', {
				name,
				value
			})
		}
	},
	computed: {
		// 将vuex的state中的所有变量，解构到全局混入的mixin中
		...mapState($uStoreKey),
		orderCode() {
			return this.$store.state.orderCodeList
		},
		rechargeOrderCode() {
			return this.$store.state.rechargeOrder
		}

	},


	data() {
		return {
			timer: null,
			module: null,
			timer1: null
		}
	},

	mounted() {
	},
	watch: {
		orderCode: {
			handler(newVal) {
				// console.log(newVal)
				this.handleOrder(newVal)
			},
			immediate: true
		},
		rechargeOrderCode: {
			handler(newVal) {
				console.log(newVal)
				this.handleRechargeOrder(newVal)
			},
			immediate: true
		}


	},
	methods: {
		handleOrder(orderCodeArg) {
			if (!orderCodeArg || orderCodeArg.length === 0) {
				clearInterval(this.timer)
				this.timer = null
				return
			}
			const app = this
			clearInterval(this.timer)
			this.timer = setInterval(() => {
				app.queryOrderState(orderCodeArg)
			}, 3000)
		},
		handleRechargeOrder(orderCodeArg) {
			if (!orderCodeArg || orderCodeArg.length === 0) {
				clearInterval(this.timer1)
				this.timer1 = null
				return
			}
			const app = this
			clearInterval(this.timer1)
			this.timer1 = setInterval(() => {
				app.queryPayStatusBatch(orderCodeArg)
			}, 3000)
		},
		queryOrderState(orderCodeArg) {
			const app = this
			console.log(orderCodeArg)
			uni.api.queryOrderList(orderCodeArg).then(res => {
				if (res.code === '200') {
					if (res.data.length === 0) {
						app.$store.commit('SAVE_ORDER_CODE', [])
					}
					let code = res.data.filter(item => item.ret.code === '201').map(ite => {
						console.log('-------------------')
						console.log(ite)
						if(ite.ret.data.payType!=='POS_BANK'){
							app.print(ite.payOrderNo, ite.ret.data, 1)
						}
						app.$store.commit('SAVE_ORDER_STATE', false)
						return {
							payOrderNo: ite.payOrderNo,
							term_id: app.sn
						}
					})

					app.$store.dispatch('deleteOrderCode', code)
					console.log(this.$store.state.orderCodeList)
				}
			})
		},
		queryPayStatusBatch(orderCodeArg) {
			const app = this
			uni.api.queryPayStatusBatch(orderCodeArg).then(res => {
				if (res.code === '200') {
					if (res.data.length === 0) {
						app.$store.commit('SAVE_RECHARGE_CODE', [])
					}
					const code = res.data.filter(item => item.payStatus === 'PAID').map(ite => {
						app.printRecharg(ite.id, ite, 3)
						return ite.id
					})
					// console.log(code)
					this.$store.dispatch('deleteRechargeOrder', code)
					// console.log(this.$store.state.rechargeOrder)
				}
			})
		},
		print(id, data, type) {

			console.log(data)
			const app = this
			uni.api.getPayOrderById(id).then(res => {
				if (res.code === '200') {
					uni.showToast({
						title: '订单支付成功！', // 提示的内容
						icon: 'none',
					})

					printData(res.data, (re) => {
						console.log(re)
					})

				}

			})
		},


		printRecharg(id, data, type) {

			console.log(data)

			const app = this
			uni.api.getRechargeById(id).then(res => {
				if (res.code === '200') {
					uni.showToast({
						title: '订单支付成功！', // 提示的内容
						icon: 'none',
					})
					printChargeData(res.data, (re) => {
						console.log(re)
					})

				}

			})
		}
	}
}